﻿namespace RabbitMQClient
{
    /// <summary>
    /// 发布订阅模式基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class PublishSubscribeBase<T> : QueueBase
        where T : class, new()
    {
        private string _exchange;
        /// <summary>
        /// mq客户端
        /// </summary>
        protected IRabbitMQClient client;

        /// <summary>
        /// 发布订阅模式基类
        /// </summary>
        /// <param name="client"></param>
        /// <param name="exchange"></param>
        public PublishSubscribeBase(IRabbitMQClient client, string exchange)
        {
            _exchange = exchange;
            this.client = client;
        }

        /// <summary>
        /// 订阅
        /// </summary>
        public abstract void Subscribe(T msg);

        /// <summary>
        /// 
        /// </summary>
        public override void Execute()
        {
            this.client.Subscribe<T>(_exchange, m =>
            {
                Subscribe(m);
            });
        }
    }
}